package com.njwb.day15.zqzb.jdbc03;

import com.njwb.day15.zqzb.util.DbUtil;

import java.sql.*;

public class QueryStudents {
    public static void query() {
        //构建连接对象
        Connection connection= null ;
        //命令器
        PreparedStatement pstmt= null ;
        //结果集
        ResultSet rs = null ;
        try {
            connection = DbUtil.getConnection();
            //3、创建命令器
            pstmt = connection.prepareStatement("select sid,sname,age,address from tb_student");
            //5.执行sql语句,返回结果集对象，并读取结果集
            rs = pstmt.executeQuery();
            //读取结果集
            while(rs.next()) {
                int sid = rs.getInt("sid");
                String sname=rs.getString("sname");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                System.out.println("编号："+sid+",姓名："+sname+",年龄："+age+",地址："+address);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
           DbUtil.closeConnection(rs,pstmt,connection);
        }

    }
}
